<?php
namespace app\app\controller;
use think\Db;
class Shop extends Common
{
    public function get_info()
    {
        $token = request()->request('token');
        $uid = token_decode($token);
        $msg['state'] = 0;
        $msg['data'] = '';
        if (empty($uid)) {
            $msg['msg'] = '参数错误';
        }else{
            $fileds = 'user.headimg,user.name,user.pid,user.aid,user.cid,user.comid,user.address';
            $data = Db::table('user user,community community')->field($fileds)->where('user.id', $uid)->find();
            $data['area'] = Db::table('province')->where('id', $data['pid'])->value('name') .Db::table('city')->where('id', $data['cid'])->value('name').Db::table('area')->where('id', $data['aid'])->value('name');
            $data['community'] = Db::table('community')->where('id', $data['comid'])->value('name');
            unset($data['pid']);
            unset($data['cid']);
            unset($data['aid']);
            unset($data['comid']);
            $msg['data'] = $data;
            $msg['state'] = 1;
            $msg['msg'] = '获取成功';
        }
        return json($msg);
    }

    public function set_info()
    {
        $token = request()->request('token');
        $uid = token_decode($token);
        $msg['state'] = 0;
        $msg['data'] = '';
        if (empty($uid)) {
            $msg['msg'] = '参数错误';
        } else {
            $name = request()->request('name');
            if (!empty($name)) {
                $data['name'] = $name;
            }
            if ($_FILES) {
                $headimg = upload();
                $data['headimg'] = $headimg['headimg']['url'];
            }
            $address = request()->request('address');
            if (!empty($address)) {
                $data['address'] = $address;
            }
            Db::table('user')->where('id', $uid)->update($data);
            $msg['state'] = 1;
            $msg['msg'] = '修改成功';
        }
        return json($msg);
    }

    public function get_goods()
    {
        $token = request()->request('token');
        $uid = token_decode($token);
        $msg['state'] = 0;
        $msg['data'] = [];
        if (empty($uid)) {
            $msg['msg'] = '参数错误';
        } else {
            $msg['state'] = 1;
            $msg['msg'] = '获取成功';
            $msg['count'] = Db::table('service')->where('uid', $uid)->count();
            $fields = 'id,title,image1,service,repertory,volume,price,status,gid,cateid,pack_charge';
            $page = !empty(request()->request('page')) ? request()->request('page') : 1;
            $pagesize = !empty(request()->request('pagesize')) ? request()->request('pagesize') : 10;
            $msg['data'] = Db::table('service')->field($fields)->where('uid', $uid)->page($page, $pagesize)->order('id desc')->select();
            foreach ($msg['data'] as $k => $v){
                $msg['data'][$k]['class_name'] = Db::table('service_category')->where('id',$v['gid'])->value('title')."-".Db::table('service_category')->where('id',$v['cateid'])->value('title');
            }
        }
        return json($msg);
    }

    public function set_status()
    {
        $token = request()->request('token');
        $uid = token_decode($token);
        $gid = request()->request('gid');
        $status = request()->request('status');
        $msg['state'] = 0;
        $msg['data'] = [];
        if (empty($uid) || empty($gid) || $status == '') {
            $msg['msg'] = '参数错误';
        } else {
            $msg['state'] = 1;
            $msg['msg'] = '操作成功';
            Db::table('service')->where(['uid' => $uid, 'id' => $gid])->update(['status' => $status]);
            $data = Db::table('service')->where(['uid' => $uid, 'id' => $gid])->value('status');
            $msg['data'] = strval($data);
        }
        return json($msg);
    }

    public function edit_goods()
    {
        $token = request()->request('token');
        $uid = token_decode($token);
        $sid = request()->request('sid');
        $data['gid'] = request()->request('gid');
        $data['cateid'] = request()->request('cateid');
        $data['title'] = request()->request('title');
        $data['price'] = request()->request('price');
        $data['repertory'] = request()->request('repertory');
        $data['type'] = request()->request('type');
        $data['pack_charge'] = request()->request('pack_charge');
        $msg['state'] = 0;
        $msg['data'] = '';
        foreach ($data as $k => $v) {
            if ($v == ''){
                unset($data[$k]);
            }
        }
        if ($uid == '') {
            $msg['msg'] = '参数错误';
        } else {
            if ($_FILES) {
                $image = upload();
                $data['image1'] = $image['image']['url'];
            }
            if ($sid == '') {
                $data['type'] = 1;
                $data['uid'] = $uid;
                Db::table('service')->insert($data);
            } else {
                Db::table('service')->where(['uid' => $uid, 'id' => $sid])->update($data);
            }
            $msg['state'] = 1;
            $msg['msg'] = '操作完成';
        }
        return json($msg);
    }

    public function del_goods()
    {
        $token = request()->request('token');
        $uid = token_decode($token);
        $gid = request()->request('gid');
        $msg['state'] = 0;
        $msg['data'] = '';
        if (empty($uid) || empty($gid)) {
            $msg['msg'] = '参数错误';
        } else {
            Db::table('service')->where(['uid' => $uid, 'id' => $gid])->delete();
            $msg['state'] = 1;
            $msg['msg'] = '操作完成';
        }
        return json($msg);
    }

    public function earning_detail()
    {
        $token = request()->request('token');
        $uid = token_decode($token);
        $msg['state'] = 0;
        $msg['data'] = [];
        if (empty($uid)) {
            $msg['msg'] = '参数错误';
        } else {
            $msg['state'] = 1;
            $msg['count'] = Db::table('earning')->where('uid',$uid)->count();
            $page = !empty(request()->request('page')) ? request()->request('page') : 1;
            $pagesize = !empty(request()->request('pagesize')) ? request()->request('pagesize') : 10;
            $msg['data'] = Db::table('earning')->field('orderid,money,time')->where(['uid'=>$uid,'type'=>'seller'])->page($page,$pagesize)->order('time desc')->select();
            $msg['msg'] = '获取成功';
        }
        return json($msg);
    }

    public function losemoney()
    {
        $token = request()->request('token');
        $uid = token_decode($token);
        $user = Db::table('user')->where('id',$uid)->find();
        $money = $user['money'];
        $msg['state'] = 0;
        $msg['data'] = '';
        if($user['money'] < $money || $money < 100){
            $msg['msg'] = '很抱歉，您的余额不满足提现条件';
        }else{
            if($user['losemoney'] != 0){
                $msg['msg'] = '您上一个提现处理还未完成，无法再进行提现';
            }else{
                $post['uid'] = $uid;
                $post['status'] = 2;
                $post['type'] = 'seller';
                $post['truename'] = $user['truename'];
                $post['account'] = $user['bank_card'];
                $post['money'] = $user['money'];
                $post['losttime'] = time();
                Db::table('losemoney')->insert($post);
                Db::table('user')->where('id',$uid)->update(['money'=>$user['money']-$money,'losemoney'=>$money]);
                $msg['state'] = 1;
                $msg['msg'] = '提现申请成功';
            }
        }
        return json($msg);
    }

    public function lose_info()
    {
        $token = request()->request('token');
        $uid = token_decode($token);
        $msg['state'] = 0;
        $msg['data'] = [];
        if(empty($uid)){
            $msg['msg'] = '参数错误';
        }else{
            $msg['state'] = 1;
            $msg['msg'] = '获取成功';
            $msg['count'] = Db::table('losemoney')->where(['uid'=>$uid,'type'=>'seller'])->count();
            $fields = 'money,status,losttime';
            $page = !empty(request()->request('page'))?request()->request('page'):1;
            $pagesize = !empty(request()->request('pagesize'))?request()->request('pagesize'):10;
            $msg['data'] = Db::table('losemoney')->field($fields)->where(['uid'=>$uid,'type'=>'seller'])->page($page,$pagesize)->order('losttime desc')->select();
        }
        return json($msg);
    }

    public function get_business()
    {
        $seller_id = request()->request('seller_id');
        $msg['state'] = 0;
        $msg['data'] = '';
        if(empty($seller_id)){
            $msg['msg'] = '参数错误';
        }else{
            $msg['state'] = 1;
            $msg['data'] = strval(Db::table('user')->where('id',$seller_id)->value('business'));
            $msg['msg'] = '获取成功';
        }
        return json($msg);
    }

    public function get_money_off()
    {
        $token = request()->request('token');
        $uid = token_decode($token);
        $msg['state'] = 1;
        $msg['data'] = Db::table('money_off')->field('off_id,full,minus')->where('seller_id',$uid)->select();
        $msg['msg'] = '获取成功';
        return json($msg);
    }

    public function edit_money_off()
    {
        $token = request()->request('token');
        $uid = token_decode($token);
        $off_id = request()->request('off_id');
        $full = request()->request('full');
        $minus = request()->request('minus');
        if($off_id == ''){
            $data['seller_id'] = $uid;
            $data['full'] = $full;
            $data['minus'] = $minus;
            Db::table('money_off')->insert($data);
        }else{
            if(!empty($full)){
                $data['full'] = $full;
            }
            if(!empty($minus)){
                $data['minus'] = $minus;
            }
            Db::table('money_off')->where(['off_id'=>$off_id,'seller_id'=>$uid])->update($data);
        }
        $msg['state'] = 1;
        $msg['msg'] = '操作成功';
        return json($msg);
    }

    public function del_money_off()
    {
        $token = request()->request('token');
        $uid = token_decode($token);
        $off_id = request()->request('off_id');
        $msg['state'] = 0;
        $msg['data'] = '';
        if($off_id == ''){
            $msg['msg'] = '参数错误';
        }else{
            $msg['state'] = 1;
            Db::table('money_off')->where(['off_id'=>$off_id,'seller_id'=>$uid])->delete();
            $msg['msg'] = '删除成功';
        }
        return json($msg);
    }



}